import Vue from 'vue'
import axios from 'axios'
import MD5 from 'blueimp-md5'
import store from '../store'
var baseSecret ='71clouddangjiang'
var currentTime = +new Date()
var token =store.state.app.loginState.token || 0
var uid=store.state.app.loginState.uid || 0
var secret =MD5(`${baseSecret}${token}${currentTime}${uid}`)
var baseUrl =`secret=${secret}&uid=${uid}&time=${currentTime}`
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

let ajax = {
    get: (url, params, responseCallback, errorCallback) => {
     return axiosRequestParams(url, 'get', params, 10000, responseCallback, errorCallback)
      // return new Promise(axiosRequestParams(url, 'get', params, 10000, responseCallback, errorCallback));
  },
  post: (url, params, responseCallback, errorCallback) => {
    return axiosRequestParams(url, 'post', params, 15000, responseCallback, errorCallback);
  },
};

function axiosRequestParams(url, method, paramsOrData, timeout, responseCallback, errorCallback, formData) {
  let params = {};
  let data = {};
  paramsOrData = paramsOrData || {};
  // paramsOrData.accessUserId = window.localStorage.getItem('uid');

return axiosRequest({
    method: method,
    url: url+`?${baseUrl}`,
    params: params,
    data: formData ? formData : data,
    timeout: 10000
  },responseCallback,errorCallback)
  // .then((res)=>{
  //   responseCallback(res)
  // })
  // .catch((err)=>{
  //   errorCallback(res)
  // })
  ;
  function axiosRequest(conf,responseCallback, errorCallback) {
    console.log(axios)
   return axios({
      method: conf.method || 'get',
      url: conf.url,
      params: conf.params || {},
      data: conf.data,
      timeout: conf.timeout || 10000
    })
      .then((res) => {
      responseCallback(res.data);
    })
      .catch((res) => {
      if (errorCallback) {
        errorCallback(res.data);
      }
    });
  }
}


export  default ajax;
